/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart.connection;

import java.util.Iterator;

import org.eclipse.uml2.uml.LiteralSpecification;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.ParameterableElement;
import org.eclipse.uml2.uml.TemplateBinding;
import org.eclipse.uml2.uml.TemplateParameter;
import org.eclipse.uml2.uml.TemplateParameterSubstitution;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.GraphEdgeEditPart;
import com.cea.papyrus.core.editpart.StereotypeDisplayEditPart;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Controller for the element that displays the tag for a edge. (ex: <<use>>)
 */
public class TagDisplayEditPart extends StereotypeDisplayEditPart {

	/**
	 * Debug channel.
	 */
	protected static final int channel=14;
	
	/**
	 * Stores the tag to be displayed.
	 */
	final private String tag;

	/**
	 * Create a new TagDisplayEditPart.
	 * 
	 * @param element 
	 * @param modelManager 
	 * @param tag 
	 * @param graphnode 
	 * @param parent 
	 */
	public TagDisplayEditPart(GraphNode graphnode,org.eclipse.uml2.uml.Element element,ModelManager modelManager, GraphEdgeEditPart parent, String tag) {
		super(graphnode, element, modelManager, parent);
		Debug.debug(this,"create TagDisplayEditPart("+tag+")",channel);
		this.tag = PapyrusPlugin.ST_LEFT+tag+PapyrusPlugin.ST_RIGHT;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.StereotypeDisplayEditPart#getTextToDisplay()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected String getTextToDisplay() {
		String text = tag;
		
		if (this.element instanceof TemplateBinding) {
			TemplateBinding bind = (TemplateBinding)element ;
			if (!bind.getParameterSubstitutions().isEmpty()) {
				text = text + "\n <" ;
				int longestString = 0 ;
				for (Iterator<TemplateParameterSubstitution> i = bind.getParameterSubstitutions().iterator() ; i.hasNext() ; ) {
					TemplateParameterSubstitution current = i.next() ;
					TemplateParameter formalParameter = current.getFormal() ;
					String formalText = "";
					if (formalParameter != null) {
						if (formalParameter.getParameteredElement() != null)
							formalText = "" + ((NamedElement)formalParameter.getParameteredElement()).getName() ;
						else
							formalText = "UNDEFINED" ;
					}
					ParameterableElement actualParameter ;
					String actualText = "" ;
					if (!current.getActuals().isEmpty()) {
						actualParameter = (ParameterableElement)current.getActuals().get(0) ;
						
						// in the case where ParameterableElement is not a named element
						if( actualParameter instanceof NamedElement){
							if( actualParameter instanceof LiteralSpecification){
								actualText=((LiteralSpecification)actualParameter).stringValue();
							}
							else{
						actualText=((NamedElement)actualParameter).getName() ;}
						}
						else{actualText="param";}
						
					}
					text = text + formalText + " -> " + actualText + "\n";
					longestString = Math.max(longestString, new String(formalText + " -> " + actualText).length()) ;
				}
				if (longestString > 0) {
					text = text.substring(0, text.length()-1) ;
					for (int i = 0 ; i<longestString/2 ; i++) {
						text = " " + text ;
					}
				}
				text = text + ">" ;
			}
			
		}
		
		// compute string for stereotypes
		String stereotypeString = super.getTextToDisplay();
		if(!("".equals(stereotypeString))) {
			text = text+"\n"+stereotypeString;
		}
		
		return text;
	}
	

}
